package com.daidai.apptoservice

import android.app.Service
import android.content.Intent
import android.os.IBinder
import kotlinx.coroutines.*
import kotlin.coroutines.EmptyCoroutineContext

class ServerService: Service() {

    var tickCounter = 0

    override fun onCreate() {
        super.onCreate()
        MyLog.info("ServerService onCreate called!")

        CoroutineScope(EmptyCoroutineContext).launch {
            launch(Dispatchers.Default){
                while (isActive){
                    tickCounter++
//                    MyLog.info("tickCounter:$tickCounter")
                    delay(1000L)
                }
            }
        }
    }

    override fun onBind(intent: Intent?): IBinder? {
        return null
    }

    override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
        MyLog.info("ServerService onStartCommand called!")

        val tickCount = intent.getIntExtra("tick",-1)
        MyLog.info("service receive tickCount:$tickCount")

        return super.onStartCommand(intent, flags, startId)
    }
}